1
|
|
|
import RouterRegistry from '@enbock/application-router/Registry'; |
2
|
|
|
import Router, {PageData} from '@enbock/application-router/Router'; |
3
|
|
|
import {Observer, ObserverAdapter} from '@enbock/state-value-observer/ValueObserver'; |
4
|
|
|
import Action from './Action'; |
5
|
|
|
import {ModulePageData} from './Application'; |
6
|
|
|
import ModuleLoader from './ModuleLoader'; |
7
|
|
|
|
8
|
|
|
describe(Action, function (): void { |
9
|
|
|
let menuOpenState: Observer<boolean>, |
10
|
|
|
routerObserver: Observer<PageData | null>, |
11
|
|
|
router: Router, |
12
|
|
|
routerRegistry: RouterRegistry, |
13
|
|
|
routerAdapter: ObserverAdapter<PageData | null>, |
14
|
|
|
moduleLoader: ModuleLoader |
15
|
|
|
; |
16
|
|
|
|
17
|
|
|
beforeEach(() => { |
18
|
|
|
menuOpenState = { |
19
|
|
|
value: false |
20
|
|
|
}; |
21
|
|
|
router = jest.genMockFromModule<Router>('@enbock/application-router/Router'); |
22
|
|
|
routerRegistry = jest.genMockFromModule<RouterRegistry>('@enbock/application-router/Registry'); |
23
|
|
|
moduleLoader = jest.genMockFromModule<ModuleLoader>('./ModuleLoader'); |
24
|
|
|
routerAdapter = { |
25
|
|
|
onChange: jest.fn() |
26
|
|
|
}; |
27
|
|
|
routerObserver = { |
28
|
|
|
value: null |
29
|
|
|
}; |
30
|
|
|
}); |
31
|
|
|
|
32
|
|
|
function createTestObject(): Action { |
33
|
|
|
return new Action( |
34
|
|
|
menuOpenState, |
35
|
|
|
routerObserver, |
36
|
|
|
router, |
37
|
|
|
routerRegistry, |
38
|
|
|
routerAdapter, |
39
|
|
|
moduleLoader |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
it('Open tab to github', () => { |
44
|
|
|
const action: Action = createTestObject(); |
45
|
|
|
|
46
|
|
|
window.open = jest.fn(); |
47
|
|
|
action.adapter.onGithubClick(); |
48
|
|
|
|
49
|
|
|
expect(window.open).toHaveBeenCalledWith('https://github.com/enbock/Time-Tracker/', '_blank'); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
it('Switch menu', () => { |
53
|
|
|
const action: Action = createTestObject(); |
54
|
|
|
|
55
|
|
|
action.adapter.onMenuClick(); |
56
|
|
|
|
57
|
|
|
expect(menuOpenState.value).toBe(true); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
it('Close menu', () => { |
61
|
|
|
menuOpenState.value = true; |
62
|
|
|
const action: Action = createTestObject(); |
63
|
|
|
|
64
|
|
|
action.adapter.onClose(); |
65
|
|
|
action.adapter.onClose(); |
66
|
|
|
|
67
|
|
|
expect(menuOpenState.value).toBe(false); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
it('Change page', () => { |
71
|
|
|
const action: Action = createTestObject(); |
72
|
|
|
const page: PageData = { |
73
|
|
|
name: 'name', |
74
|
|
|
baseUrl: 'rootUrl', |
75
|
|
|
currentUrl: 'url' |
76
|
|
|
}; |
77
|
|
|
routerRegistry.getPages = jest.fn().mockReturnValue([page]); |
78
|
|
|
router.changePage = jest.fn(); |
79
|
|
|
menuOpenState.value = true; |
80
|
|
|
|
81
|
|
|
action.adapter.onMenu('name'); |
82
|
|
|
expect(router.changePage).toHaveBeenCalledWith(page); |
83
|
|
|
expect(menuOpenState.value).toBeFalsy(); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
it('Change to not registered page will be ignored', function (): void { |
87
|
|
|
const action: Action = createTestObject(); |
88
|
|
|
const page: PageData = { |
89
|
|
|
name: 'name', |
90
|
|
|
baseUrl: 'rootUrl', |
91
|
|
|
currentUrl: 'url' |
92
|
|
|
}; |
93
|
|
|
routerRegistry.getPages = jest.fn().mockReturnValue([page]); |
94
|
|
|
router.changePage = jest.fn(); |
95
|
|
|
menuOpenState.value = true; |
96
|
|
|
|
97
|
|
|
action.adapter.onMenu('notRegistered'); |
98
|
|
|
expect(router.changePage).not.toHaveBeenCalled(); |
99
|
|
|
expect(menuOpenState.value).toBeTruthy(); |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
it('Load module on page change', async () => { |
103
|
|
|
const action: Action = createTestObject(); |
104
|
|
|
const page: ModulePageData = { |
105
|
|
|
module: './New/Module', |
106
|
|
|
name: 'name', |
107
|
|
|
baseUrl: 'rootUrl', |
108
|
|
|
currentUrl: 'url' |
109
|
|
|
}; |
110
|
|
|
moduleLoader.loadModule = jest.fn().mockResolvedValue(undefined); |
111
|
|
|
|
112
|
|
|
await action.adapter.onPageChanged(page); |
113
|
|
|
expect(moduleLoader.loadModule).toHaveBeenCalledWith('./New/Module'); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
it('Load page config and initialize module loader with home page', () => { |
117
|
|
|
routerRegistry.registerPage = jest.fn(); |
118
|
|
|
router.changePage = jest.fn(); |
119
|
|
|
const action: Action = createTestObject(); |
120
|
|
|
action.loadPageConfig(); |
121
|
|
|
|
122
|
|
|
const homePage: ModulePageData = { |
123
|
|
|
name: 'home', |
124
|
|
|
baseUrl: './', |
125
|
|
|
currentUrl: './', |
126
|
|
|
module: './HelloWorld' |
127
|
|
|
}; |
128
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith(homePage); |
129
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith({ |
130
|
|
|
name: 'settings', |
131
|
|
|
baseUrl: './settings/', |
132
|
|
|
currentUrl: './settings/', |
133
|
|
|
module: './Settings/Settings' |
134
|
|
|
}); |
135
|
|
|
expect(router.changePage).toBeCalledWith(homePage); |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
it('Load page config and initialize module loader with last page', () => { |
139
|
|
|
const homePage: ModulePageData = { |
140
|
|
|
name: 'home', |
141
|
|
|
baseUrl: './', |
142
|
|
|
currentUrl: './', |
143
|
|
|
module: './HelloWorld' |
144
|
|
|
}; |
145
|
|
|
const lastPage: ModulePageData = { |
146
|
|
|
name: 'settings', |
147
|
|
|
baseUrl: './settings/', |
148
|
|
|
currentUrl: './settings/', |
149
|
|
|
module: './Settings/Settings' |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
routerRegistry.registerPage = jest.fn(); |
153
|
|
|
router.changePage = jest.fn(); |
154
|
|
|
routerAdapter.onChange = jest.fn(); |
155
|
|
|
routerObserver.value = lastPage; |
156
|
|
|
|
157
|
|
|
const action: Action = createTestObject(); |
158
|
|
|
action.loadPageConfig(); |
159
|
|
|
|
160
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith(homePage); |
161
|
|
|
expect(routerRegistry.registerPage).toBeCalledWith(lastPage); |
162
|
|
|
expect(router.changePage).not.toHaveBeenCalled(); |
163
|
|
|
expect(routerAdapter.onChange).toHaveBeenCalledWith(lastPage); |
164
|
|
|
}); |
165
|
|
|
}); |
166
|
|
|
|